<?php

require_once 'config/magiclist.php';

class army
{
    protected $units = array();
    protected $original_units = array();

    public function __construct()
    {

    }
    
    public function add_unit($unit)
    {
        $this->units[] = $unit;
        $this->original_units[] = clone $unit;
    }

    public function add_invocation($unit)
    {
        $this->units = array_merge(array($unit), $this->units);
    }

    public function get_current_stack()
    {
        return reset($this->units);
    }

    public function update_remaining_units($remaining)
    {
        reset($this->units);
        $current = each($this->units);
        if ($remaining <= 0)
        {
            unset($this->units[$current['key']]);
        }
        else
        {
            $this->units[$current['key']]->quantity = $remaining;
        }
    }

    public function get_xp()
    {
        $xp = 0;
        foreach ($this->original_units as $stack)
        {
            $xp += $stack->get_xp();
        }
        return $xp;
    }

    public function get_power()
    {
        $power = 0;
        foreach ($this->original_units as $stack)
        {
            $power += $stack->power * $stack->quantity;
        }
        return $power;
    }

    public function is_empty()
    {
        return !count($this->units);
    }

    public function count()
    {
        return count($this->units);
    }

    public function apply_magic($power, $value_type, $multiple = FALSE)
    {
        if ($multiple)
        {
            foreach ($this->units as &$u)
            {
                $this->apply_magic_to_stack($power, $value_type, $u);
            }
        }
        else
        {
            $this->apply_magic_to_stack($power, $value_type, $this->get_current_stack());
        }
    }

    protected function apply_magic_to_stack($power, $value_type, &$stack)
    {
        if ($value_type == _MAGIC_VALUE_TYPE_RATIO_)
        {
            $power *= ($stack->power / 100) * $stack->quantity;
        }
        else if ($value_type == _MAGIC_VALUE_TYPE_EACH_)
        {
            $power *= $stack->quantity;
        }
        echo $power . ' : ' . $stack->quantity . ' => ';
        $stack->quantity = ceil((($stack->power * $stack->quantity) + $power) / $stack->power);
        echo $stack->quantity . '<br />';
    }
}
?>
